package leetCode.firstWeek;

public class Code_31 {
  public void nextPermutation(int[] nums) {
    int start = 0, min = Integer.MAX_VALUE, min_index = -1;
    for (int i = nums.length - 1; i > 0; i--) {
      if (nums[i] > nums[i - 1]) {
        for (int j = i - 1; j < nums.length; j++) {
          if (nums[j] > nums[i - 1] && nums[j] < min) {
            min = nums[j];
            min_index = j;
          }
        }
        int tmp = nums[min_index];
        nums[min_index] = nums[i - 1];
        nums[i - 1] = tmp;
        start = i; // 从i开始冒泡排序
        break;
      }
    }
    for (int i = start; i < nums.length - 1; i++) {
      for (int j = start; j < nums.length - 1; j++) {
        if (nums[j] > nums[j + 1]) {
          int tmp = nums[j];
          nums[j] = nums[j + 1];
          nums[j + 1] = tmp;
        }
      }
    }
  }
}
